package com.ouc.openplatform.common;
import com.github.abel533.mapper.MapperProvider;
import com.github.abel533.mapperhelper.EntityHelper;
import com.github.abel533.mapperhelper.EntityHelper.EntityColumn;
import com.github.abel533.mapperhelper.MapperHelper;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.ibatis.jdbc.SqlBuilder;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.scripting.xmltags.ForEachSqlNode;
import org.apache.ibatis.scripting.xmltags.MixedSqlNode;
import org.apache.ibatis.scripting.xmltags.SqlNode;
import org.apache.ibatis.scripting.xmltags.StaticTextSqlNode;

public class CommonMapperImpl extends MapperProvider
{
  public CommonMapperImpl(Class<?> mapperClass, MapperHelper mapperHelper)
  {
    super(mapperClass, mapperHelper);
  }
  
  public SqlNode deleteByIDS(MappedStatement ms)
  {
    Class<?> entityClass = getSelectReturnType(ms);
    Set<EntityHelper.EntityColumn> entityColumns = EntityHelper.getPKColumns(entityClass);
    EntityHelper.EntityColumn entityColumn = null;
    Iterator<EntityColumn> i$ = entityColumns.iterator();
    if (i$.hasNext())
    {
      EntityHelper.EntityColumn entity = (EntityHelper.EntityColumn)i$.next();
      entityColumn = entity;
    }
    EntityHelper.EntityColumn column = entityColumn;
    List<SqlNode> sqlNodes = new ArrayList<SqlNode>();
    
    SqlBuilder.BEGIN();
    
    SqlBuilder.DELETE_FROM(tableName(entityClass));
    
    String sql = SqlBuilder.SQL();
    
    sqlNodes.add(new StaticTextSqlNode(sql + " WHERE " + column.getColumn() + " IN "));
    
    SqlNode foreach = new ForEachSqlNode(ms.getConfiguration(), new StaticTextSqlNode("#{" + column.getProperty() + "}"), "ids", "index", column.getProperty(), "(", ")", ",");
    
    sqlNodes.add(foreach);
    return new MixedSqlNode(sqlNodes);
  }
}
